<template>
  <div id="list">
    <el-table
      :data="goodslists"
      style="width: 100%"
      row-key="id"
      border
      lazy
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="商品编号" >
      </el-table-column>
      <el-table-column prop="goodsname" label="商品名称" >
      </el-table-column>
      <el-table-column prop="price" label="商品价格" >
      </el-table-column>
      <el-table-column prop="market_price" label="市场价格" >
      </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <div class="imgprops">
            <img :src="$pre + scope.row.img" alt="" >
          </div>
        </template>
      </el-table-column>
       <el-table-column label="是否新品">
        <template slot-scope="scope">
          <div>
            <el-button type="success" v-if="scope.row.isnew == 1"
              >启用</el-button
            >
            <el-button type="danger" v-else>禁用</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否热卖">
        <template slot-scope="scope">
          <div>
            <el-button type="success" v-if="scope.row.ishot == 1"
              >启用</el-button
            >
            <el-button type="danger" v-else>禁用</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <div>
            <el-button type="success" v-if="scope.row.status == 1"
              >启用</el-button
            >
            <el-button type="danger" v-else>禁用</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div>
            <el-button type="primary" plain @click="edit(scope.row.id)"
              >编 辑</el-button
            >
            <el-button type="primary" @click="del(scope.row.id)"
              >删 除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
     <el-pagination background layout="prev, pager, next" :total="goodstotal" :page-size="goodssize" @current-change="change">
    </el-pagination>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { successAlert, warningAlert } from "../../../util/alert";
import { goodsdelete } from "../../../requset/api";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      goodslists: "goods/list",
      goodstotal: "goods/total",
      goodssize: "goods/size",
    }),
  },
  methods: {
    ...mapActions({
      reqChangePage: "goods/reqChangePage",
      reqChangeTotal:"goods/reqChangeTotal"
    }),
    edit(id) {
      this.$emit("edit", id);
    },
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "success",
      })
        .then(() => {
          goodsdelete({ id: id }).then((res) => {
            if (res.data.code == 200) {
              successAlert(res.data.msg);
              this.reqChangePage(1)
            } else {
              warningAlert(res.data.msg);
            }
          });
        })
        .catch(() => {
          warningAlert("已取消删除");
        });
    },
     change(num){
      this.reqChangePage(num)
    },
  },
  mounted() {
    this.reqChangePage(1),
    this.reqChangeTotal()
  },
};
</script>
<style lang="less" scoped>
.imgprops{
  img{
    width: 150px;
    height: 150px;
  }
}
</style>